package com.example.home.film;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.film.adapter.FilmBannerAdapter;
import com.example.home.film.adapter.FilmListAdapter;
import com.example.home.film.bean.FilmBannerBean;
import com.example.home.film.bean.FilmListBean;
import com.example.home.tool.OKHttpTool;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class FilmMainActivity extends AppCompatActivity {

    private ImageView back;
    private SearchView houseSearch;
    private Button butHouse;
    private ViewPager vpBanner;
    private RecyclerView rcvList;
    private Button btnMore;
    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;
    private FilmBannerAdapter filmBannerAdapter;
    private FilmListAdapter filmListAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_film_main);

        initView();
        //轮播图
        getBanner();
        startAutoScroll();

        //电影列表
        getFilmList();
        
        getSearchFilm();
    }


    @Override
    protected void onResume() {
        super.onResume();
        getFilmList();
    }

    private void getSearchFilm() {
        houseSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 在用户提交查询时调用
                performSearch(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    private void performSearch(String query) {
        OKHttpTool.doNetRequest("prod-api/api/movie/film/list?name="+query,
                OKHttpTool.GET,
                FilmListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        FilmListBean filmListBean = (FilmListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // 在数据集合中进行模糊匹配
                                List<FilmListBean.RowsBean> matchedList = new ArrayList<>();
                                for (FilmListBean.RowsBean item : filmListBean.getRows()) {
                                    if (item.getName().toLowerCase().contains(query.toLowerCase())) {
                                        matchedList.add(item);
                                    }
                                }

                                // 如果有匹配项，可以选择展示在界面上
                                if (!matchedList.isEmpty()) {
                                    // 更新RecyclerView
                                    updateRecyclerView(matchedList);
                                    // 清空搜索框的文本和焦点
                                    houseSearch.setQuery("", false);
                                    houseSearch.setIconified(true);
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void updateRecyclerView(List<FilmListBean.RowsBean> matchedList) {
        // 设置匹配的电影列表到Adapter
        filmListAdapter.setFilmList(matchedList);
        // 通知Adapter数据集发生变化
        filmListAdapter.notifyDataSetChanged();
    }




    private void getFilmList() {
        OKHttpTool.doNetRequest("prod-api/api/movie/film/list",
            OKHttpTool.GET,
            FilmListBean.class,
            new OKHttpTool.NetCallback() {
                @Override
                public void onSuccess(Object obj) {
                    FilmListBean filmListBean = (FilmListBean) obj;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            bindList(filmListBean);
                        }
                    });
                }

                @Override
                public void onFailure(String msg) {

                }
            });
    }

    private void bindList(FilmListBean filmListBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_list);
        filmListAdapter = new FilmListAdapter(FilmMainActivity.this,filmListBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(FilmMainActivity.this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(filmListAdapter);
    }

    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/movie/rotation/list",
                OKHttpTool.GET,
                FilmBannerBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        FilmBannerBean filmBannerBean = (FilmBannerBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannershow(filmBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindBannershow(FilmBannerBean filmBannerBean) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        filmBannerAdapter = new FilmBannerAdapter(FilmMainActivity.this, filmBannerBean.getRows());
        viewPager.setAdapter(filmBannerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == filmBannerAdapter.getCount()) {
                    currentPage = 0;
                }
                vpBanner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }



    private void initView() {

        back = findViewById(R.id.back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FilmMainActivity.this.finish();
            }
        });

        houseSearch = findViewById(R.id.house_search);

        butHouse = findViewById(R.id.but_house);
        vpBanner = findViewById(R.id.vp_banner);
        rcvList = findViewById(R.id.rcv_list);
        btnMore = findViewById(R.id.btn_more);

        btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (filmListAdapter != null){
                    btnMore.setVisibility(View.GONE);
                    filmListAdapter.setShowAllData(true);
                }
            }
        });

    }
}